package com.ljx.itbbs.server.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ljx.itbbs.common.domain.entity.UserEntity;
import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.common.domain.vo.resp.PageResp;
import com.ljx.itbbs.common.utils.ServletUtil;
import com.ljx.itbbs.server.user.domain.vo.req.PlaterSettingReq;
import com.ljx.itbbs.server.user.domain.vo.req.UserPageReq;
import com.ljx.itbbs.server.user.domain.vo.req.UserUpdateReq;
import com.ljx.itbbs.server.user.domain.vo.resp.MenuResp;
import com.ljx.itbbs.server.user.domain.vo.resp.admin.UserInfoAdminResp;
import com.ljx.itbbs.server.user.domain.vo.resp.UserInfoResp;
import com.ljx.itbbs.server.user.service.MenuService;
import com.ljx.itbbs.server.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public ApiResult<UserInfoResp> userInfo(@RequestParam("uid") Long uid){
        UserEntity userEntity = userService.getUserInfoById(uid);
        return ApiResult.success(BeanUtil.copyProperties(userEntity, UserInfoResp.class));
    }

    @ApiOperation("修改用户信息")
    @PutMapping("/updateInfo")
    public ApiResult<Void> updateUserInfo(@Valid @RequestBody UserUpdateReq req){
        userService.updateUserInfo(ServletUtil.getUserId(), req);

        return ApiResult.success();
    }

    @ApiOperation("获取用户菜单列表")
    @GetMapping("/menu/list")
    public ApiResult<List<MenuResp>> getUserMenuList() {
        List<MenuResp> tbMenuList = menuService.getUserMenuList(ServletUtil.getUserId());

        return ApiResult.success(tbMenuList);
    }

    @ApiOperation("管理员获取用户列表")
    @GetMapping("/admin/list")
    public ApiResult<PageResp<UserInfoAdminResp>> adminUserList(@Valid UserPageReq req){

        return ApiResult.success(userService.adminUserList(req));
    }

    @ApiOperation("管理员根据账号查找用户信息")
    @GetMapping("/admin/search/info")
    public ApiResult<UserInfoResp> searchUserInfo(@RequestParam("username") String username){
        UserEntity userEntity = userService.selectByUsername(username);

        return ApiResult.success(BeanUtil.copyProperties(userEntity, UserInfoResp.class));
    }

    @ApiOperation("管理员修改用户状态")
    @PutMapping("/admin/update/status/{userId}/{status}")
    public ApiResult<Void> adminUpdateUserStatus(@PathVariable("userId") Long userId,
                                                 @PathVariable("status") Integer status){
        userService.adminUpdateUserStatus(userId, status);

        return ApiResult.success();
    }

    @ApiOperation("管理员设置版主")
    @PostMapping("/admin/setting/plater")
    public ApiResult<Void> adminSettingPlater(@Valid @RequestBody PlaterSettingReq req){
        userService.adminSettingPlater(req);

        return ApiResult.success();
    }

    @ApiOperation("管理员取消版主")
    @PutMapping("/admin/cancel/plater")
    public ApiResult<Void> adminCancelPlater(@Valid @RequestBody PlaterSettingReq req){
        userService.adminCancelPlater(req);

        return ApiResult.success();
    }


}
